/**
 * 相关人员实体类
 * 定义AI侦探应用中的案件相关人员数据结构
 */
export class Person {
  // 人员ID
  id: string;
  
  // 姓名
  name: string;
  
  // 角色类型
  roleType: 'suspect' | 'victim' | 'witness' | 'expert' | 'other';
  
  // 性别
  gender?: 'male' | 'female' | 'other';
  
  // 年龄
  age?: number;
  
  // 身份证号
  idNumber?: string;
  
  // 联系电话
  phone?: string;
  
  // 邮箱
  email?: string;
  
  // 地址
  address?: string;
  
  // 描述
  description?: string;
  
  // 关联案件ID列表
  relatedCaseIds: string[];
  
  // 关联证据ID列表
  relatedEvidenceIds: string[];
  
  // 关联线索ID列表
  relatedClueIds: string[];
  
  // 创建时间
  createdAt: Date;
  
  // 更新时间
  updatedAt: Date;
  
  // AI分析结果
  aiAnalysis?: {
    riskAssessment: 'low' | 'medium' | 'high';
    relationshipAnalysis: {
      [key: string]: string; // 与其他人的关系
    };
    behaviorPatterns?: string[];
  };
  
  /**
   * 构造函数
   * @param personData 人员数据
   */
  constructor(personData: {
    id?: string;
    name: string;
    roleType: 'suspect' | 'victim' | 'witness' | 'expert' | 'other';
    gender?: 'male' | 'female' | 'other';
    age?: number;
    idNumber?: string;
    phone?: string;
    email?: string;
    address?: string;
    description?: string;
    relatedCaseIds?: string[];
    relatedEvidenceIds?: string[];
    relatedClueIds?: string[];
    createdAt?: Date;
    updatedAt?: Date;
    aiAnalysis?: {
      riskAssessment: 'low' | 'medium' | 'high';
      relationshipAnalysis: {
        [key: string]: string;
      };
      behaviorPatterns?: string[];
    };
  }) {
    this.id = personData.id || this.generateId();
    this.name = personData.name;
    this.roleType = personData.roleType;
    this.gender = personData.gender;
    this.age = personData.age;
    this.idNumber = personData.idNumber;
    this.phone = personData.phone;
    this.email = personData.email;
    this.address = personData.address;
    this.description = personData.description;
    this.relatedCaseIds = personData.relatedCaseIds || [];
    this.relatedEvidenceIds = personData.relatedEvidenceIds || [];
    this.relatedClueIds = personData.relatedClueIds || [];
    this.createdAt = personData.createdAt || new Date();
    this.updatedAt = personData.updatedAt || new Date();
    this.aiAnalysis = personData.aiAnalysis;
  }
  
  /**
   * 生成唯一ID
   * @returns 唯一ID字符串
   */
  private generateId(): string {
    return `person_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }
  
  /**
   * 添加关联案件
   * @param caseId 案件ID
   */
  addRelatedCase(caseId: string): void {
    if (!this.relatedCaseIds.includes(caseId)) {
      this.relatedCaseIds.push(caseId);
      this.updatedAt = new Date();
    }
  }
  
  /**
   * 添加关联证据
   * @param evidenceId 证据ID
   */
  addRelatedEvidence(evidenceId: string): void {
    if (!this.relatedEvidenceIds.includes(evidenceId)) {
      this.relatedEvidenceIds.push(evidenceId);
      this.updatedAt = new Date();
    }
  }
  
  /**
   * 添加关联线索
   * @param clueId 线索ID
   */
  addRelatedClue(clueId: string): void {
    if (!this.relatedClueIds.includes(clueId)) {
      this.relatedClueIds.push(clueId);
      this.updatedAt = new Date();
    }
  }
  
  /**
   * 转换为JSON对象
   * @returns JSON格式的人员数据
   */
  toJSON() {
    return {
      id: this.id,
      name: this.name,
      roleType: this.roleType,
      gender: this.gender,
      age: this.age,
      idNumber: this.idNumber,
      phone: this.phone,
      email: this.email,
      address: this.address,
      description: this.description,
      relatedCaseIds: this.relatedCaseIds,
      relatedEvidenceIds: this.relatedEvidenceIds,
      relatedClueIds: this.relatedClueIds,
      createdAt: this.createdAt.toISOString(),
      updatedAt: this.updatedAt.toISOString(),
      aiAnalysis: this.aiAnalysis
    };
  }
  
  /**
   * 从JSON对象创建Person实例
   * @param json JSON格式的人员数据
   * @returns Person实例
   */
  static fromJSON(json: any): Person {
    return new Person({
      id: json.id,
      name: json.name,
      roleType: json.roleType,
      gender: json.gender,
      age: json.age,
      idNumber: json.idNumber,
      phone: json.phone,
      email: json.email,
      address: json.address,
      description: json.description,
      relatedCaseIds: json.relatedCaseIds || [],
      relatedEvidenceIds: json.relatedEvidenceIds || [],
      relatedClueIds: json.relatedClueIds || [],
      createdAt: json.createdAt ? new Date(json.createdAt) : new Date(),
      updatedAt: json.updatedAt ? new Date(json.updatedAt) : new Date(),
      aiAnalysis: json.aiAnalysis
    });
  }
}